package com.zsrt.day20170424.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.CharBuffer;
import java.text.FieldPosition;

import org.junit.Test;

import com.zsrt.day20170405.ch01.TestDemo02;

/**
*@author jzh
*@创建时间 2017年4月24日上午10:16:16
*类说明：
*/
public class InputStreamTest {
	
	
	@Test
	public void TestDemo02(){
		File file=new File("E:/a/TestSwitch.java");
		InputStream is=null;
		try {
			is = new FileInputStream(file);
			byte[] b=new byte[3];
			int len=-1;
			while((len=is.read(b))!=-1){
				String st=new String(b, 0, len);
				System.out.print(st);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if(null!=is){
				is.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	@Test 
	public void Test1() throws IOException{
		 File file=new File("E:/a/c.jpg");
		 InputStream is=new FileInputStream(file);
		 OutputStream os=new FileOutputStream("E:/a/c.txt");
		 byte[] b=new byte[3];
		os.write(b);
		 int a=-1;
		 while((a=is.read())!=-1){
			 System.out.println((char)a);
		 }
		 is.close();
	 }
	
	@Test
	public void Test3(){
		File file1=new File("C:\\Users\\zsrt\\Desktop\\1.png");
		File file2=new File("C:\\Users\\zsrt\\Desktop\\2.png");
		InputStream is=null;
		OutputStream os=null;
		try{
			is=new FileInputStream(file1);
			os=new FileOutputStream(file2);
			byte[] b=new byte[20];
			int len=-1;
			while((len=is.read(b))!=-1){
				os.write(b, 0, len);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@Test
	public void test4() throws IOException{
		File file1=new File("iotest/a.txt");
		File file2=new File("iotest/b.txt");
		
		InputStream is=new FileInputStream(file1);
		OutputStream os=new FileOutputStream(file2);
		
		byte[] b=new byte[3];
		is.read(b);
		os.write(b);
		
		InputStreamReader isr=new InputStreamReader(is);
		OutputStreamWriter osw=new OutputStreamWriter(os);
		char[] cbuf=new char[3];
		isr.read(cbuf);
		osw.write(new String("hello"));
		
		BufferedReader br=new BufferedReader(isr);
		BufferedWriter bw=new BufferedWriter(osw);
		br.readLine();
		bw.write(new String("hello"));
		
		br.close();
		bw.close();
	}
	

}
